package com.yuanqi.dz.dc.service.impl;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import pnbclient.helper.StringHelper;
import pnbclient.util.PnbLogFactory;
import pnblog.PnbLogging;

import com.yuanqi.dz.dc.adapters.PreReadFile;
import com.yuanqi.dz.dc.info.Main;
import com.yuanqitech.advancedpower.powerdate.DateUtil;
import com.yuanqitech.advancedpower.powerdate.PowerDateFormat;
import com.yuanqitech.advancedpower.powernet.NetConnection;
import com.yuanqitech.powermop.dz.DZDataInterface;
import com.yuanqitech.powermop.dz.DZInfo;
import com.yuanqitech.powermop.dz.DZInfoArray;
/**
 * @author zjt
 *@version 1.0
 */
public class PreDZServiceImpl  implements DZDataInterface{
	@SuppressWarnings("unused")
	private String  version="1.0";
	
	public static final PnbLogging LOG = PnbLogFactory.getLogger("PRE_DZ_LOG");

	public static void main(String[] args) {
		DZInfoArray  dz= new PreDZServiceImpl().getDZData("105420180620344","20161020");
		DZInfo[]  d= dz.getDzInfo();
		if(null!=d){
			for (int i = 0; i < d.length; i++) {
				System.out.println(d[i].toString());
			}   
		}
		System.out.println(dz.getRc());
	}

	public DZInfoArray getDZData(String merchantnum, String dzdate) {
		DZInfoArray dzInfoArray = new DZInfoArray();
		Main  main =  new Main().init(merchantnum);
		String realtype = main.getRealSourceType();
		if(null == realtype){
			LOG.addError("获取对账原始数据实现方式未配置");
			dzInfoArray.setRc("-1");
			dzInfoArray.setErrormsg("获取对账原始数据实现方式未配置");
			dzInfoArray.setDzInfo(new DZInfo[0]);
			return dzInfoArray;
		}else if("1".equals(realtype)){

		}else if("2".equals(realtype)){
			String real_ftp_ip=main.getRealAddress();
			String real_ftp_port=main.getRealFtpPort();
			String real_username=main.getRealUsername();
			String  real_password=main.getRealPassword();
			String real_filename=main.getRealFileName();
			String real_ftp_filepath=main.getRealRemotePath();
			String real_dz_date_format=main.getRealDzDateFormat();
			String real_local_path=main.getRealLocalPath();

			String pre_type=main.getPreSourceType();
			String pre_ftp_ip=main.getPreAddress();
			String pre_ftp_port=main.getRealFtpPort();
			String pre_username=main.getPreUsername();
			String  pre_password=main.getPrePassword();
			String pre_filename=main.getPreFileName();
			String pre_ftp_filepath=main.getPreRemotePath();
			String pre_dz_date_format=main.getPreDzDateFormat();
			String pre_local_path=main.getPreLocalPath();

			SimpleDateFormat sdf = new SimpleDateFormat(real_dz_date_format);
			Calendar calendar = Calendar.getInstance();
			if (StringHelper.isEmpty(dzdate)) {
				calendar.add(Calendar.DAY_OF_MONTH, Integer.valueOf("-1".toString()));
			} else {
				calendar.setTime(PowerDateFormat.getInstance(DateUtil.YYYYMMDD).parse(dzdate));
			}
			String filename = real_filename.replace("{merchant}", merchantnum).replace("{date}", sdf.format(calendar.getTime()));
			File file=new File(real_local_path, filename);
			boolean download = false;
			int port = StringHelper.isEmpty(real_ftp_port) ? 0 : Integer.valueOf(real_ftp_port);
			try {
				download = new NetConnection(real_ftp_ip, port, real_username, real_password, real_ftp_filepath).ftpDownload(file);
				if (!download) {

					LOG.addError("ftp上未找到对账文件，开始获取预对账数据");
					dzInfoArray=getPreDZData(pre_type, pre_ftp_ip, pre_ftp_port, pre_username, pre_password, pre_filename, pre_ftp_filepath, pre_dz_date_format, pre_local_path, dzdate, merchantnum);
				}else{
					dzInfoArray=  new PreReadFile().getDzdata(file, merchantnum, dzdate);
					if(null!=dzInfoArray.getRc() && "-1".equals(dzInfoArray.getRc())){
						LOG.addTrace("从原始对账文件中未读取到对账数据,开始获取预对账数据");
						dzInfoArray=getPreDZData(pre_type,pre_ftp_ip, pre_ftp_port, pre_username, pre_password, pre_filename, pre_ftp_filepath, pre_dz_date_format, pre_local_path, dzdate,merchantnum);
					}
				}
			}catch (Exception e) {
				LOG.addError("获取对账数据异常"+e);
				dzInfoArray=getPreDZData(pre_type,pre_ftp_ip, pre_ftp_port, pre_username, pre_password, pre_filename, pre_ftp_filepath, pre_dz_date_format, pre_local_path, dzdate,merchantnum);
				return dzInfoArray;
			}

		}
		DZInfo[]  dz=	dzInfoArray.getDzInfo();
		int n=0;
		if(null!=dz && dz.length>0){
			n=dz.length;
			if(LOG.isDebug()){
				if(n>10){
					for (int i = 0; i < 10; i++) {
						LOG.addDebug(dz[i].toString());
					}
				}else{
					for (int i = 0; i < dz.length; i++) {
						LOG.addDebug(dz[i].toString());
					}
				}

			}
		}
		LOG.addTrace("获取对账数据"+n+"条");


		return dzInfoArray;
	}

	public  DZInfoArray  getPreDZData(String pre_type,String pre_ftp_ip,String pre_ftp_port,String pre_username,String pre_password,String pre_filename,String pre_ftp_filepath,String pre_dz_date_format,String pre_local_path,String dzdate,String merchantnum){
		DZInfoArray dzInfoArray=new DZInfoArray();
		if(null==pre_type){
			LOG.addError("预对账获取对账数据方式未配置");
			dzInfoArray.setRc("-1");
			dzInfoArray.setErrormsg("预对账获取对账数据方式未配置");
			dzInfoArray.setDzInfo(new DZInfo[0]);
			return dzInfoArray;
		}else if("2".equals(pre_type)){
			SimpleDateFormat sdf = new SimpleDateFormat(pre_dz_date_format);
			Calendar calendar = Calendar.getInstance();
			if (StringHelper.isEmpty(dzdate)) {
				calendar.add(Calendar.DAY_OF_MONTH, Integer.valueOf("-1".toString()));
			} else {
				calendar.setTime(PowerDateFormat.getInstance(DateUtil.YYYYMMDD).parse(dzdate));
			}
			String filename = pre_filename.replace("{merchant}", merchantnum).replace("{date}", sdf.format(calendar.getTime()));
			File file=new File(pre_local_path, filename);
			boolean download = false;
			int port = StringHelper.isEmpty(pre_ftp_port) ? 0 : Integer.valueOf(pre_ftp_port);
			try {
				download = new NetConnection(pre_ftp_ip, port, pre_username, pre_password, pre_ftp_filepath).ftpDownload(file);
				if (!download) {
					LOG.addError("ftp未找到对账文件"+file.getName());
					dzInfoArray.setRc("-1");
					dzInfoArray.setErrormsg("ftp未找到对账文件"+file.getName());
					dzInfoArray.setDzInfo(new DZInfo[0]);
				}
				dzInfoArray=  new PreReadFile().getDzdata(file, merchantnum, dzdate);
				String  rc=dzInfoArray.getRc();
				if(null!=rc && rc.equals("1")){
					rc="10";
					dzInfoArray.setRc(rc);
				}
			}catch (Exception e) {
				LOG.addError("获取对账数据异常"+e);
				dzInfoArray.setRc("-1");
				dzInfoArray.setErrormsg("获取对账数据异常");
				dzInfoArray.setDzInfo(new DZInfo[0]);
				return dzInfoArray;

			}
		}
		return dzInfoArray;

	}

}
